package uoc.biblioteca.mappings.dao;

// Generated 11/04/2011 14:10:24 by Hibernate Tools 3.2.1.GA

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import uoc.biblioteca.mappings.object.HistoricExcelsVo;
import uoc.biblioteca.sessionFactory.HibernateSessionFactory;

/**
 * Home object for domain model class HistoricExcels.
 * @see uoc.biblioteca.mappings.dao.HistoricExcelsVo
 * @author Hibernate Tools
 */
public class HistoricExcelsHome extends BaseHome {

	private static final Log log = LogFactory.getLog(HistoricExcelsHome.class);

	private final SessionFactory sessionFactory = null;


	public HistoricExcelsVo findById(java.lang.Integer id) {
		log.debug("getting HistoricExcelsVo instance with id: " + id);
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			HistoricExcelsVo instance = (HistoricExcelsVo) session.get("uoc.biblioteca.mappings.object.HistoricExcelsVo", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
				
			}
			
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public void saveNew(HistoricExcelsVo instance) {
		
		log.debug("save new HistoricExcels instance");
		
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			session.clear();
			
			tx = session.beginTransaction();
			
			session.saveOrUpdate(instance);
			
			tx.commit();
			session.close();
			
			log.debug("saveNew successful");
			
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	
	public void delete(HistoricExcelsVo instance) {
		
		log.debug("delete HistoricExcelsVo instance");
		
		try {
			
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();
			
			session.clear();
			
			tx = session.beginTransaction();
			session.delete(instance);
			//session.saveOrUpdate(instance);
			
			tx.commit();
			session.close();
			
			log.debug("delete successful");
			
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	//Mostrar todos los proveedores
	public HistoricExcelsVo[] getAllHistoricExcels() {
		
		/* will hold the books we are going to return later */
		try{
			List HistoricExcelss = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.HistoricExcelsVo as c order by c.nameProveedor ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				HistoricExcelsVo HistoricExcelsTemp = (HistoricExcelsVo) iter.next();
				HistoricExcelss.add(HistoricExcelsTemp);
			}
			tx.commit();
			
			return (HistoricExcelsVo[]) HistoricExcelss.toArray(new HistoricExcelsVo[0]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//Obtener historic per el nom del fitxer
	public HistoricExcelsVo findHistoricByFileName(String fileCreate) {
		
		/* will hold the books we are going to return later */
		try{
			HistoricExcelsVo HistoricExcels = null;
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			tx = session.beginTransaction();
			
			String hql="";
			hql="select c from uoc.biblioteca.mappings.object.HistoricExcelsVo as c where c.fileCreate = '"+fileCreate+"'  ";
			
			List tmpEvents = session.createQuery(hql).list();
			for (Iterator iter = tmpEvents.iterator(); iter.hasNext();) {
				HistoricExcels= (HistoricExcelsVo) iter.next();
				
			}
			tx.commit();
			
			return HistoricExcels;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//Obtener el historics por fecha
	public HistoricExcelsVo[] findHistoricExcelsLast() {
		
		/* will hold the books we are going to return later */
		try{
			List historicExcelss = new ArrayList();
		
			/* a Hibernate session */
			Session session = null;
			/* we always need a transaction */
			Transaction tx = null;
			/* get session of the current thread */
			session = HibernateSessionFactory.currentSession();

			session.flush();
			
			// Create HQL Between clause
            String HQL_QUERY = "select c from uoc.biblioteca.mappings.object.HistoricExcelsVo as c where c.dateGenerate < :start ";

            Query query = session.createQuery(HQL_QUERY);

            //Obtener fecha actual
            Date dataTo = new Date();
            Calendar hoy = Calendar.getInstance();
            hoy.add(Calendar.MONTH, -6);
            dataTo=hoy.getTime();
            query.setParameter("start", dataTo);
            List result = query.list();
			
			
			for (Iterator iter = result.iterator(); iter.hasNext();) {
				HistoricExcelsVo historicExcelsTemp = (HistoricExcelsVo) iter.next();
				historicExcelss.add(historicExcelsTemp);
				
			}
			
			if(result!=null){
				return (HistoricExcelsVo[]) historicExcelss.toArray(new HistoricExcelsVo[0]);
			}
			
			
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
